package 数组的相对排序;
//计数排序
public class Solution2 {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int upper = 0;
        for (int x : arr1) {
            upper = Math.max(upper,x);
        }
        int[] frequency = new int[upper + 1];

        for (int x : arr1) {
            frequency[x]++;
        }

        int[] ans = new int[arr1.length];
        int index = 0;

        for (int y : arr2) {
            for (int i = 0; i < frequency[y]; i++) {
                ans[index++] = y;
            }
            frequency[y] =0;
        }

        for (int i = 0; i <= upper; i++) {
            for (int j = 0; j < frequency[i]; j++) {
                ans[index++] = i;
            }
        }
        return ans;

    }
}
